package com.lavida.project.system.remind.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.DateUtils;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.page.TableDataInfo;
import com.lavida.project.system.remind.domain.Remind;
import com.lavida.project.system.remind.mapper.RemindMapper;
import com.lavida.project.system.remind.service.RemindService;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.mapper.UserMapper;
import com.lavida.project.system.user.service.IUserService;

@Controller
@RequestMapping("/system/remind")
public class RemindController  extends BaseController{
	Logger logger = LoggerFactory.getLogger(getClass());
	private String prefix = "system/remind";
	@Autowired
	private RemindService remindService;
	@Autowired
    private UserMapper userMapper;
	@Autowired
	private RemindMapper remindMapper;
	
	@Autowired
	private IUserService userService;

    @GetMapping()
    public String remind(HttpServletRequest request,ModelMap mmap) {
    	System.out.println("id="+request.getParameter("id"));
        mmap.put("currentUser", ShiroUtils.getSysUser());
        User user = ShiroUtils.getSysUser();
        User user1 = userService.selectUserById(user.getUserId());
        mmap.put("user", user1);
        mmap.put("id", request.getParameter("id"));
        return prefix + "/remind";
    }
    
    /**
     * 查看提醒
     */
    @GetMapping("/view/{id}")
    @ResponseBody
    public Map<String,Object> edit(@PathVariable("id") String id)
    {
		Remind mc = remindService.getRemind(id);
		User remindUser = userMapper.selectUserByLoginName(mc.getRemind());
		mc.setRemindName(remindUser.getUserName());
		mc.setRemindDeptName(remindUser.getDept().getDeptName());
        Map<String,Object> result = new HashMap<String,Object>();
    	result.put("remind", mc);
        return result;
    }
    
    
    
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Remind remind)
    {
    	//管理员可以查看所有提醒
    	if (!User.isAdmin(ShiroUtils.getUserId())) {
    		remind.setReceiver(ShiroUtils.getLoginName());
        }
        startPage();
        List<Remind> list = remindService.getRemindList(remind);
       //处理一下人员姓名和部门名称
        for(Remind remind_1:list) {
			User remindUser = userMapper.selectUserByLoginName(remind_1.getRemind());
			System.out.println("user>>>>>>>"+remindUser.toString());
			remind_1.setRemindName(remindUser.getUserName());
			remind_1.setRemindDeptName(remindUser.getDept().getDeptName());
			System.out.println("remind>>>>>>>."+remind_1.toString());
			User receiver = userMapper.selectUserByLoginName(remind_1.getReceiver());
			System.out.println(receiver.toString());
			remind_1.setReceiverName(receiver.getUserName());
			//System.out.println("list>>>>>>>>>>>>>>>>"+list.toString());
		}
        return getDataTable(list);
    }
   
	
	
	@RequestMapping("/getRemindListNoPaging")
	@ResponseBody
	public List<Remind> getRemindListNoPaging(HttpServletRequest request) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("receiver",ShiroUtils.getLoginName());
		paraMap.put("isView", "0");
		List<Remind> list = remindService.getRemindListNoPaging(paraMap);
		System.out.println("list>>>>>>>>>>>>>>>>>>"+list.toString());
		return list;
	}
	
	

	/**
	 * 查看单笔提醒
	 * @param request
	 * @return
	 */
	@RequestMapping("/getRemind")
	@ResponseBody
	public Remind getRemind(HttpServletRequest request) {
		String id = request.getParameter("id");
		Remind mc = remindService.getRemind(id);
		User remindUser = userMapper.selectUserByLoginName(mc.getRemind());
		mc.setRemindName(remindUser.getUserName());
		mc.setRemindDeptName(remindUser.getDept().getDeptName());
		return mc;
	}

	/**
	 * 修改为已读
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(HttpServletRequest request) {
		String id = request.getParameter("id");
		String loginName = ShiroUtils.getLoginName();
		Remind remind = remindService.getRemind(id);
		if(remind.getReceiver().equals(loginName)) {
			Remind remind2 =  new Remind();
			remind2.setId(Long.parseLong(id));
			remind2.setIsView("1");
			remindService.edit(remind2);
		}
 		return "success";
	}
	/**
	 * 发票申请单 综合管理部节点推送消息给维保部负责人
	 * @param request
	 * @return
	 */
	@RequestMapping("/sendInvoiceRemind")
	@ResponseBody
	public Map<String,Object> sendInvoiceRemind(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String msg = "";
		String gmUserId = request.getParameter("gmUserId");
		String keyNo = request.getParameter("keyNo");
		String companyNames =  request.getParameter("companyNames");
		String formName = "发票申请单";
		Integer remindCount = remindMapper.getRemindCountByCondi(formName, keyNo);
		if(remindCount>0) {
			msg = "这笔发票申请单的提醒已推送过维保部负责人，无法重复推送！";
		}else {
			Remind remind = new Remind();
			remind.setRemind(gmUserId);
			String wbDeptLeaderId = userMapper.selectUserByUniqueRolekey("wbDeptLeader");
			remind.setReceiver(wbDeptLeaderId);
			User finance = userMapper.selectUserByLoginName(gmUserId);
			remind.setRemindDept(finance.getDeptId()+"");
			remind.setRemindDate(DateUtils.getTime());
			remind.setFormUrl("invoiceForm/edit/" + keyNo);
			remind.setFormName(formName);
			remind.setRemindContent("公司名称为["+companyNames+"]的这些公司的发票到了，请来我这里拿发票！！！");
			remind.setIsView("0");
			int add = remindService.add(remind);
			if(add<1) {
				msg = "提醒推送失败，请联系系统管理员！";
			}else {
				msg = "提醒推送成功！";
			}
		}
		result.put("msg", msg);
		
		return result;
		
	}
	



	
}
